import {
  getPackage_items,
  createShare,
  getCardInfo,
  sendCardHandle,
  userHandlePackage,
  userPayment
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    selectItemsArr: [], //已选择的检测项目
    packageName: '', //套餐名
    packagePrice: '', //套餐价格
    packageEndTime: '', //套餐有效期
    packageRemark: '', //套餐说明
    id: "", //套餐id 
    dataindex: 0, //当前选中选项卡  0套餐 1充值卡
    target: "",
    org_price: "",

    cardPrice: "", //充值金额
    cardGivePrice: "", //赠送金额
    cardRemark: "", //赠送金额
    cardEndTime: "", //充值卡有效期
    cardId: "", //会员卡id
    read: false,
    isShowReckoning: false
  },
  handleZanStepperChange(e) {
    const componentId = e.target.dataset.componentId;
    const stepper = e.detail;
    this.setData({
      [`selectItemsArr[${componentId}].count`]: stepper,
    });
  },
  //跳转至添加项目
  goAddProItem: function() {
    wx.navigateTo({
      url: '../addproitem/addproitem?items=' + JSON.stringify(this.data.items),
    })
  },
  //切换选项卡
  toShowhidden: function(e) {
    if (e.currentTarget.dataset.index == this.data.dataindex) {

    } else {
      this.setData({
        dataindex: e.currentTarget.dataset.index
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    wx.hideShareMenu({})
    let temp = options.tagindex + 1 + '_' + options.id;
    createShare(temp, (r) => {
      if (r.code == 0) {
        self.setData({
          ppid: r.data.ppid
        })
      }
    })

    self.setData({
      options: options
    })
    // options.sended=true;
    // options.id=70;
    // options.mtype=2; 
    if (options.sended) {
      //推送过来的
      userHandlePackage(options.id, options.mtype, (r) => {
        if (r.code == 0) {
          self.setData({
            dataindex: options.mtype == 1 ? 0 : 1,
            //  read: options.read ? options.read : false
          })
          if (options.mtype == 1) {
            //套餐
            if (options.item) {
              wx.setNavigationBarTitle({
                title: options.read ? "查看套餐" : "发送套餐"
              })
              let itemInfo = r.data
              console.log(itemInfo)
              self.setData({
                // items: r.data,
                selectItemsArr: itemInfo.children,
                packageName: itemInfo.title, //套餐名
                packagePrice: itemInfo.price, //套餐价格
                packageEndTime: itemInfo.end, //套餐有效期
                packageRemark: itemInfo.description, //套餐说明
                target: itemInfo.target,
                paid: itemInfo.paid,
                id: itemInfo.id,
                org_price: itemInfo.org_price,
                // qc: options.qc
              })

            } else {
              self.setData({
                // items: r.data
              })
            }
          } else if (options.mtype == 2) {
            //充值卡

            //如果是点击项目进来。自动填上信息展示项目
            if (options.item) {
              wx.setNavigationBarTitle({
                title: "发送充值卡"
              })
              let itemInfo = r.data
              self.setData({
                cardPrice: itemInfo.charge, //充值金额,
                cardGivePrice: itemInfo.give, //赠送金额
                cardRemark: itemInfo.description, //充值卡说明
                cardEndTime: itemInfo.end, //充值卡有效期
                cardId: itemInfo.id,
                paid: itemInfo.paid,
                // qc: options.qc
              })

            }
          }

        } else {
          wx.showToast({
            icon: 'none',
            title: r.message
          })
        }
      })

    } else {

      self.setData({
        dataindex: options.tagindex ? options.tagindex : 0,
        read: options.read ? options.read : false
      })
      if (this.data.dataindex == 0) {
        //套餐
        if (options.item) {
          wx.setNavigationBarTitle({
            title: options.read ? "查看套餐" : "发送套餐"
          })
          let itemInfo = JSON.parse(options.item)
          console.log(itemInfo)
          self.setData({
            // items: r.data,
            selectItemsArr: itemInfo.children,
            packageName: itemInfo.title, //套餐名
            packagePrice: itemInfo.price, //套餐价格
            packageEndTime: itemInfo.end, //套餐有效期
            packageRemark: itemInfo.description, //套餐说明
            target: itemInfo.target,
            id: itemInfo.id,
            org_price: itemInfo.org_price,
            // qc: options.qc
          })

        } else {
          self.setData({
            // items: r.data
          })
        }
      } else if (this.data.dataindex == 1) {
        //充值卡

        //如果是点击项目进来。自动填上信息展示项目
        if (options.item) {
          wx.setNavigationBarTitle({
            title: "发送充值卡"
          })
          let itemInfo = JSON.parse(options.item)

          self.setData({
            cardPrice: itemInfo.charge, //充值金额,
            cardGivePrice: itemInfo.give, //赠送金额
            cardRemark: itemInfo.description, //充值卡说明
            cardEndTime: itemInfo.end, //充值卡有效期
            cardId: itemInfo.id,
            // qc: options.qc
          })

        }
      }
    }




  },
  onShow: function() {

  },

  //关闭结算窗口
  onReckoningClose: function() {
    this.setData({
      isShowReckoning: false
    })
    wx.navigateBack()
  },
  //保存套餐
  onSave: function() {
    let self = this;
    let {
      qc,
      id,
      packageName
    } = this.data;
    userPayment(packageName, 1, id, (r) => {
      if (r.code == 0) {
        debugger;
        wx.requestPayment({
          'timeStamp': r.data.timeStamp,
          'nonceStr': r.data.nonceStr,
          'package': r.data.package,
          'signType': r.data.signType,
          'paySign': r.data.paySign,
          'success': function(res) {
            // wx.redirectTo({
            //   url: '../../usercenter/usercenter',
            // })
            self.setData({
              isShowReckoning: true
            })
          },
          'fail': function(res) {}
        })
      }
    })
  },
  //保存充值卡
  onSaveCard: function() {
    let {
      qc,
      cardId
    } = this.data;
    userPayment("充值卡", 2, cardId, (r) => {
      if (r.code == 0) {
        wx.requestPayment({
          'timeStamp': r.data.timeStamp,
          'nonceStr': r.data.nonceStr,
          'package': r.data.package,
          'signType': r.data.signType,
          'paySign': r.data.paySign,
          'success': function(res) {

            self.setData({
              isShowReckoning: true
            })
          },
          'fail': function(res) {}
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {


    if (res.from === 'button') {
      // 来自页面内转发按钮

    }

    return {
      title: '快来助力我，获取免费洗车券。',
      path: 'pages/sharebox/xiche/xiche?ppid=' + this.data.ppid,
      imageUrl: 'http://www.haoche666.com/api/app_pics_url?name=share_chat'
    }


  }
})